package com.yangjun.controller.businessAnalysis.flowAnalysis;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yangjun.common.ServerResponse;
import com.yangjun.service.bussiness.ChannelConversionService;


/**
 * 
 * @ClassName ChannelConversionController
 * @Description 渠道转化
 * @author Administrator
 * @Date 2017年1月12日 下午2:26:13
 * @version 1.0.0
 */
@Controller
@RequestMapping("/businessAnalysis/channelConversion")
public class ChannelConversionController {
	private static Logger logger = Logger.getLogger(ChannelConversionController.class);  
	@Autowired
	private ChannelConversionService channelConversionService;
	
	@ResponseBody
	@RequestMapping(value="/getSite",method =RequestMethod.GET)
	public Object getSite(@RequestParam(value = "userid",required = true)String userid){
		return channelConversionService.getSite(userid);
	}
	
	@ResponseBody
	@RequestMapping(value="/getChannel1TableData",method =RequestMethod.GET)
	public List<Map<String,String>> getChannel1TableData(
			@RequestParam(value = "startDay",required = false)String startDay,
			@RequestParam(value = "endDay",required = false)String endDay,
			@RequestParam(value = "siteid",required = false)String siteid,
			@RequestParam(value = "utm_medium",required = false)String utm_medium,
			@RequestParam(value = "utm_source",required = false)String utm_source,
			@RequestParam(value = "sort",required = false)String sort){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		List<Map<String,String>> table1Data = null;
		if(StringUtils.isEmpty(startDay)&&StringUtils.isEmpty(startDay)){
			 //获取前月的第一天
			Calendar   calendar=Calendar.getInstance();//获取当前日期 
			calendar.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
			startDay = format.format(calendar.getTime());
			calendar.setTime(new Date());
			calendar.add(Calendar.DAY_OF_MONTH,-1);
			endDay = format.format(calendar.getTime());
		}
		table1Data = channelConversionService.getChannel1Data(startDay,endDay,siteid,utm_medium,utm_source,sort);
		return table1Data;
	}
	@ResponseBody
	@RequestMapping("/getChannel2TableData")
	public List<Map<String,String>> getChannel2TableData(
			@RequestParam(value = "startDay",required = false)String startDay,
			@RequestParam(value = "endDay",required = false)String endDay,
			@RequestParam(value = "siteid",required = false)String siteid,
			@RequestParam(value = "utm_medium",required = false)String utm_medium,
			@RequestParam(value = "utm_source",required = false)String utm_source,
			@RequestParam(value = "sort",required = false)String sort){
		return channelConversionService.getChannel2Data(startDay,endDay,siteid,utm_medium,utm_source,sort);
	}
	@ResponseBody
	@RequestMapping("/getUtmSourceDimension")
	public List<String> getUtmSourceDimension(@RequestParam(value = "siteid",required = true)String siteid,
			@RequestParam(value = "utm_medium",required = false)String utm_medium){
		return channelConversionService.queryUtmSourceDimension(siteid,utm_medium);
	}
	@ResponseBody
	@RequestMapping("/getUtmMediumDimension")
	public Object getUtmMediumDimension(@RequestParam(value = "siteid",required = true)String siteid,
			@RequestParam(value = "userid",required = true)String userid){
		
		return ServerResponse.createBySuccess(channelConversionService.queryUtmMediumDimension(siteid,userid));
	}

	@ResponseBody
	@RequestMapping("/getLastMonthData")
	public List<Map<String,String>> getLastMonthData(
			@RequestParam(value = "siteid",required = false)String siteid,
			@RequestParam(value = "utm_medium",required = false)String utm_medium,
			@RequestParam(value = "utm_source",required = false)String utm_source,
			@RequestParam(value = "sort",required = false)String sort){
		String beginDay = null;
		String endDay = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH,1);
		String startDayOfMonth = format.format(calendar.getTime());
		
		calendar.setTime(new Date());
		String today = format.format(calendar.getTime());
		
		if(today.equals(startDayOfMonth)){
			calendar.add(Calendar.MONTH, -1);
			calendar.set(Calendar.DAY_OF_MONTH,1);
			beginDay = format.format(calendar.getTime());
			calendar.add(Calendar.MONTH, 1);
			calendar.add(Calendar.DAY_OF_MONTH,-1);
			endDay = format.format(calendar.getTime());
		}else{
			calendar.set(Calendar.DAY_OF_MONTH,-30);
			beginDay = format.format(calendar.getTime());
			endDay = today;
		}
		return channelConversionService.queryLastMonthData(beginDay,endDay,siteid,utm_medium,utm_source,sort);
	}
	
	@ResponseBody
	@RequestMapping("/getLastYearData")
	public List<Map<String,String>> getLastYearData(
			@RequestParam(value = "siteid",required = false)String siteid,
			@RequestParam(value = "utm_medium",required = false)String utm_medium,
			@RequestParam(value = "utm_source",required = false)String utm_source,
			@RequestParam(value = "sort",required = false)String sort){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		String endOfMonth = format.format(calendar.getTime());
		calendar.set(Calendar.MONTH,-11);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		String startOfMonth = format.format(calendar.getTime());
		System.out.println(startOfMonth+"===="+endOfMonth);
//		System.out.println(utm_source);
		return channelConversionService.queryLastYearData(startOfMonth,endOfMonth,siteid,utm_medium,utm_source,sort);
	}
	
	@ResponseBody
	@RequestMapping("/getLastTwoMonthData")
	public Map<String,Object> getLastTwoMonthData(
			@RequestParam(value = "siteid",required = false)String siteid,
			@RequestParam(value = "utm_medium",required = false)String utm_medium,
			@RequestParam(value = "utm_source",required = false)String utm_source){
//		System.out.println("最近两个月"+siteid+"===="+utm_medium+"===="+utm_source);
		return channelConversionService.queryLastTwoMonthData(siteid,utm_medium,utm_source);
	}
	
	@ResponseBody
	@RequestMapping("/getLastTwoYearData")
	public Map<String,Object> getLastTwoYearData(
			@RequestParam(value = "siteid",required = false)String siteid,
			@RequestParam(value = "utm_medium",required = false)String utm_medium,
			@RequestParam(value = "utm_source",required = false)String utm_source){
//		System.out.println("最近两年"+siteid+"===="+utm_medium+"===="+utm_source);
		return channelConversionService.queryLastTwoYearData(siteid,utm_medium,utm_source);
	}
}
